<?php


namespace App\Http\Controllers\Admin\SystemSet\AppVersion;


use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use App\Response\Response;
use App\Services\Tools\FileService;
use Illuminate\Http\Request;

class VersionSettingController extends BaseController
{
    /**
     * @api {get} /system-set/app-version/version-setting app版本设置模块列表
     * @apiVersion 0.1.0
     * @apiName 系统设置-app版本设置模块列表
     * @apiGroup 系统设置
     *
     * @apiParam {number} page 当前页数 可选
     * @apiParam {number} perPage 分页容量 可选
     * @apiParam {number} app_type app类型 1 安卓  2ios
     *
     *
     * @apiSuccessExample 成功响应
     *
     *
    {
    "code": 200,
    "data": {
    "list": [
    {
    "id(序号)": 1,
    "management_id(app管理版本id)": 1,
    "app_type(app类型 1 安卓  2ios)": 1,
    "android_application_type(安卓应用类型 1 气泡语音  2气泡星球)": 2,
    "version(版本号)": "v2.2",
    "version_status(ios控制开关 1 开  2关)": 1,
    "setting_content(设置内容json 键值对)": "[{\"channels\":2,\"model\":2}]",
    "add_time(创建时间)": "2021-06-25 16:17:42",
    "update_time(修改时间)": "2021-06-25 16:45:38"
    }
    ],
    "meta": {
    "page": 1,
    "limit": "10",
    "next": null,
    "total": 1
    }
    }
    }
     *
     *
     */
    public function list(Request $request)
    {
        //参数校验
        try {
            $this->validate($request, [
                'app_type' => 'filled|numeric',
                'perPage' => 'filled|numeric',
                'page' => 'filled|numeric',
            ]);
        } catch (\Exception $e) {
            throw new BasicException(10001, json_encode($e->errors()));
        }

        $list = app('repo_version_management')->getVersionSettingList($request->input());

        return Response::sendList($list);
    }

    /**
     * @api {post} /system-set/app-version/version-setting app版本设置模块创建
     * @apiVersion 0.1.0
     * @apiName 系统设置-app版本设置模块创建
     * @apiGroup 系统设置
     *
     * @apiParam {number} app_type                      app类型 1 安卓  2ios
     * @apiParam {number} android_application_type      安卓应用类型 1 气泡语音  2气泡星球
     * @apiParam {string} version                       版本号
     * @apiParam {number} version_status                ios控制开关 1 开  2关
     * @apiParam {string} setting_content               设置内容json
     * @apiParam {string} management_id                 app管理版本id 注：下拉选择好版本号返回的主键id
     *
     * @apiSuccessExample 成功响应
     *
     *      {"code":200,"data":true}
     *
     * @apiError 10001 参数校验失败
     *
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function create(Request $request)
    {
        //参数校验
        $v = app('validator')->make($request->all(),
            [
                'app_type' => 'filled|in:1,2',
                'android_application_type' => 'in:1,2,3,4',
                'version' => 'filled',
                'management_id' => 'filled|numeric',
                'setting_content' => 'filled',
                'version_status' => 'filled|in:1,2',
            ],
            []);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());
        $params = $request->only(['app_type', 'android_application_type', 'version', 'management_id', 'setting_content', 'version_status']);
        $params['add_time'] = date('Y-m-d H:i:s');

        $rows = app('repo_version_management')->addAppVersionSetting($params);
        if (!$rows) {
            throw new BasicException(10001, "插入失败！");
        }

        return Response::sendData(true);
    }

    /**
     * @api {put} /system-set/app-version/version-setting app版本设置模块修改
     * @apiVersion 0.1.0
     * @apiName 系统设置-app版本设置模块修改
     * @apiGroup 系统设置
     *
     *
     * @apiParam {number} id
     * @apiParam {number} app_type                      app类型 1 安卓  2ios
     * @apiParam {number} android_application_type      安卓应用类型 1 气泡语音  2气泡星球
     * @apiParam {string} version                       版本号
     * @apiParam {number} version_status                ios控制开关 1 开  2关
     * @apiParam {string} setting_content               设置内容json
     * @apiParam {string} management_id                 app管理版本id 注：下拉选择好版本号返回的主键id
     *
     * @apiSuccessExample 成功响应
     *
     *      {"code":200,"data":true}
     *
     * @apiError 10001 参数校验失败
     *
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function update(Request $request)
    {
        //参数校验
        try {
            $this->validate($request, [
                'id' => 'required|integer',
                'app_type' => 'required|in:1,2',
                'android_application_type' => 'in:1,2,3,4',
                'version' => 'required',
                'version_status' => 'required|in:1,2',
            ]);
        } catch (\Exception $e) {
            throw new BasicException(10001, json_encode($e->errors()));
        }

        $id = $request->input('id');

        $res = app('repo_version_management');

        $params = $request->only(['app_type', 'android_application_type', 'version', 'management_id', 'setting_content', 'version_status']);

        $rows = $res->saveAppVersionSetting(["id" => $id], $params);
        if (!$rows) {
            throw new BasicException(10001, "更新失败！");
        }

        return Response::sendData(true);
    }

    /**
     * @api {delete} /system-set/app-version/version-setting app版本设置模块删除
     * @apiVersion 0.1.0
     * @apiName 系统设置-app版本设置模块删除
     * @apiGroup 系统设置
     *
     * @apiParam {string} id                       删除id 必传
     *
     * @apiSuccessExample 成功响应
     *
     *      {"code":200,"data":true}
     *
     * @apiError 10001 参数校验失败
     *
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function del(Request $request) {
        //参数校验
        try {
            $this->validate($request, [
                'id' => 'required|int',
            ]);

        } catch (\Exception $e) {
            throw new BasicException(10001, json_encode($e->errors()));
        }

        $id = $request->input("id");

        $rows = app('repo_version_management')->delAppVersionSetting($id);
        if (!$rows) {
            throw new BasicException(10001, "删除失败！");
        }

        return Response::sendData(true);
    }
}